package payment;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeliSendServlet extends HttpServlet {
	private final String URL_FOR_INQUIRY = "http://itensv:8080/Delivery/Report";//問い合わせ先の URL 

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		perform(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		perform(req, resp);
	}
	private void perform(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		try { 
		URL url = new URL(URL_FOR_INQUIRY); 
		 //システムに接続する 
		 HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
		 //POST メソッドで送信する準備 
		 connection.setRequestMethod("POST"); 
		 connection.setDoOutput(true); 
		 PrintWriter out = new PrintWriter(connection.getOutputStream()); 
		 
		 StringBuffer param = new StringBuffer(); 
		 //問い合わせパラメータ 
		 param.append("c_code=iten7"); 
		 param.append("&c_password=f7Y4aBTi"); 
		 param.append("&r_url=http://localhost:8080/itenVII/payment/CashManageServlet"); 
//		 param.append("&r_date =" + “20140315”); //省略可 
//		 http://192.168.3.190:8080
		 out.print(param.toString()); 
		out.flush(); 
		//結果を取得する 
		if (connection != null) { 
		if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
		 //送信完了 
//			System.out.print("送信完了");
		 } 
		 else { 
		 //送信失敗 
		 } 
		 } 
		 //後処理 
		 if (out != null) { 
		 out.close(); 
		 } 
		 if (connection != null) { 
		 connection.disconnect(); 
		 } 
		} 
		catch (MalformedURLException e1) { 
		 e1.printStackTrace(); 
		} 
		catch (ProtocolException e1) {  e1.printStackTrace();
		} 
		catch (IOException e1) { 
		 e1.printStackTrace(); 
		} 
//		http://192.168.3.190:8080
		resp.sendRedirect("/itenVII/operator/cashManage.jsp");
	}
}
